package com.kkd.kkdapp.activity;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.MyHappyCoinAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.MyHappyCoinData;
import com.kkd.kkdapp.http.entity.MyHappyCoinDataItem;
import com.kkd.kkdapp.http.entity.MyHappyCoinItemBean;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpClient;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.kkd.kkdapp.pinnedsection.PinnedSectionRefreshListView;
import com.kkd.kkdapp.pinnedsection.PinnedSectionRefreshListView.IXListViewListener;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.DialogUtils;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.view.CountView;

/**
 * 新版本我的快乐币页面，代入购买
 */
public class MyHappyCoinActivity extends BaseActivity implements
		IXListViewListener {

	private Context mContext = this;

	private final int SUC_REQUEST = 1001;
	private final int FAIL_REQUEST = 1002;

	private View back;
	private View myHappyBiNew_tv_howto;
	private View happy_currency_task;
	private CountView tv_num_happyBi_head;
	private View new_happycoin;
	private View toHappyShop;
	private View toBuyHappyCoin;
	private ImageView img_null;
	private TextView tv_null;
	private PinnedSectionRefreshListView mListView;
	private MyHappyCoinAdapter mAdapter;
	private ArrayList<MyHappyCoinItemBean> list_bean = new ArrayList<MyHappyCoinItemBean>();
	private ArrayList<MyHappyCoinDataItem> datalist = new ArrayList<MyHappyCoinDataItem>();
	private int pageNO = 1;
	private int pageSize = 20;
	private boolean REFRESHFLAG = true;
	private String coinShopUrl;// 快乐商城url
	private String coinMisUrl;// 快乐币任务url
	private int buyCoinFlag = 0;// 是否可购买快乐币标识 ---1为可购买
	private Dialog loadDialog;

	private boolean isShowToastOnRefresh = false;

	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SUC_REQUEST:
				if (loadDialog != null) {
					loadDialog.cancel();
					loadDialog = null;
				}
				MyHappyCoinData data = (MyHappyCoinData) msg.obj;
				if (data != null) {
					if (REFRESHFLAG) {
						// 快乐币数量
						tv_num_happyBi_head.showNumberWithAnimation(0,
								data.getCoinCount(), 1000);
					}
					buyCoinFlag = data.getBuyCoinFlag();
					if (buyCoinFlag == 1) {// 展示新版快乐比商城及其购买入口
						new_happycoin.setVisibility(View.VISIBLE);
					} else {
						new_happycoin.setVisibility(View.GONE);
					}
					coinShopUrl = data.getCoinShopUrl();
					coinMisUrl = data.getCoinMisUrl();
					if (TextUtils.isEmpty(coinMisUrl)) {
						myHappyBiNew_tv_howto.setVisibility(View.VISIBLE);
						happy_currency_task.setVisibility(View.GONE);
					} else {
						happy_currency_task.setVisibility(View.VISIBLE);
						myHappyBiNew_tv_howto.setVisibility(View.GONE);
					}

					int pageNo = data.getPageNO();
					int pageCount = data.getPageCount();
					if (pageNo > pageCount) {
						if (pageNO == 1) {
							mListView.stopLoadMore(-1);
						} else {
							mListView.stopLoadMore(1);
						}
					} else {
						mListView.setNoMore(false);
						if (pageNO == 1) {
							datalist.clear();
						}
						datalist.addAll(data.getDataList());
						list_bean.clear();
						list_bean.addAll(MyHappyCoinItemBean.getData(datalist));
						mAdapter.notifyDataSetChanged();
						if (data.getDataList().size() < pageSize) {
							mListView.stopLoadMore(1);
						} else {
							mListView.stopLoadMore(0);
						}
					}
					mListView.stopRefresh();
				}

				if (REFRESHFLAG) {
					if (datalist == null || datalist.size() == 0) {
						img_null.setImageResource(R.drawable.img_null);
						tv_null.setText("暂无明细");
						img_null.setVisibility(View.VISIBLE);
						tv_null.setVisibility(View.VISIBLE);
					} else {
						img_null.setVisibility(View.GONE);
						tv_null.setVisibility(View.GONE);
					}
				}

				if (!isShowToastOnRefresh) {
					isShowToastOnRefresh = true;
				}
				break;

			case FAIL_REQUEST:
				if (loadDialog != null) {
					loadDialog.cancel();
					loadDialog = null;
				}
				if (pageNO == 1) {
					if (datalist != null && datalist.size() > 0) {
						mListView.stopLoadMore(1);
					} else {
						mListView.stopLoadMore(-1);
					}
				} else {
					mListView.stopLoadMore(1);
				}
				mListView.stopRefresh();

				if (datalist == null || datalist.size() == 0) {
					img_null.setImageResource(R.drawable.img_null_net);
					if (msg.arg1 == KkdHttpClient.NO_NET) {
						tv_null.setText("网络错误，请稍候下拉刷新重试");
					} else {
						tv_null.setText("服务异常，请稍候下拉刷新重试");
					}
					img_null.setVisibility(View.VISIBLE);
					tv_null.setVisibility(View.VISIBLE);
				}

				if (isShowToastOnRefresh) {
					String desc = (String) msg.obj;
					if (!TextUtils.isEmpty(desc)) {
						AppUtil.makeToastForCustomer(mContext, desc);
					}
				} else {
					isShowToastOnRefresh = true;
				}
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_happy_coin);
		initView();
		initListener();
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (loadDialog != null) {
			loadDialog.cancel();
			loadDialog = null;
		}
		loadDialog = DialogUtils.toLoadDialog(mContext, "加载中...");
		mListView.setSelection(0);
		pageNO = 1;
		REFRESHFLAG = true;
		requestData();
	}

	private void initView() {
		back = findViewById(R.id.happyBiRecord_top_img_back);
		myHappyBiNew_tv_howto = findViewById(R.id.myHappyBiNew_tv_howto);
		happy_currency_task = findViewById(R.id.happy_currency_task);
		img_null = (ImageView) findViewById(R.id.img_null);
		tv_null = (TextView) findViewById(R.id.tv_null);
		// 快乐币数量
		tv_num_happyBi_head = (CountView) findViewById(R.id.tv_num_happyBi_head);
		tv_num_happyBi_head.setDecimalFormat(new DecimalFormat("#,###"));
		// 新版本快乐币入口
		new_happycoin = findViewById(R.id.new_happycoin);
		// 前往快乐商城
		toHappyShop = findViewById(R.id.toHappyShop);
		// 前往购买快乐币
		toBuyHappyCoin = findViewById(R.id.toBuyHappyCoin);
		// 下方listview
		mListView = (PinnedSectionRefreshListView) findViewById(R.id.mListView);
		mListView.setDividerHeight(0);
		mAdapter = new MyHappyCoinAdapter(list_bean, mContext);
		mListView.setAdapter(mAdapter);
		mListView.setPullLoadEnable(true);
		mListView.setXListViewListener(this);
	}

	private void initListener() {
		back.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				finish();
			}
		});
		myHappyBiNew_tv_howto.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				SystemUtils.onEvent("Account_HappyCoin_HowToEarn", 1);
				Intent intent = new Intent(mContext,
						HowToHappyCoinActivity.class);
				startActivity(intent);
			}
		});
		happy_currency_task.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				if (!TextUtils.isEmpty(coinMisUrl)) {
					SystemUtils.onEvent("Account_HappyCoin_HowToEarn", 1);
					Intent intent = new Intent(mContext, KKDWebActivity.class);
					intent.putExtra("url", coinMisUrl);
					startActivity(intent);
				}
			}
		});
		// 前往快乐商城
		toHappyShop.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				if (!TextUtils.isEmpty(coinShopUrl)) {
					SystemUtils.onEvent("Account_HappyCoin_GoToHappyMarket", 1);
					Intent intent = new Intent(mContext, KKDWebActivity.class);
					intent.putExtra("titleName", "快乐商城");
					intent.putExtra("url", coinShopUrl);
					intent.putExtra("coinMisUrl", coinMisUrl);
					startActivity(intent);
				}
			}
		});
		// 前往购买快乐币
		toBuyHappyCoin.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				if (buyCoinFlag == 1) {
					Intent intent = new Intent(mContext,
							HappyMoneyBuyActivity.class);
					startActivity(intent);
				} else {
					AppUtil.makeToastForCustomer(mContext, "功能上线中");
				}
			}
		});
	}

	/**
	 * 请求接口数据
	 */
	private void requestData() {
		KkdHttpMagager.getInstance().getMyHappyCoinManager(pageNO, pageSize,
				MyHappyCoinData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						Message msg = Message.obtain();
						msg.what = SUC_REQUEST;
						msg.obj = responseBody;
						mHandler.sendMessage(msg);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = Message.obtain();
						msg.what = FAIL_REQUEST;
						msg.obj = desc;
						msg.arg1 = statusCode;
						mHandler.sendMessage(msg);
					}
				});
	}

	@Override
	public void onRefresh() {
		pageNO = 1;
		REFRESHFLAG = true;
		requestData();
	}

	@Override
	public void onLoadMore() {
		pageNO++;
		REFRESHFLAG = false;
		requestData();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (loadDialog != null) {
			loadDialog.cancel();
			loadDialog = null;
		}
	}
}
